using System;
using System.Collections.Generic;
using System.Web.UI;
using OpenWaves.Navigation.Controls;
using OpenWaves.Web;

namespace OpenWaves.Navigation
{
    [Obsolete("Use NavigationView control with Model property set to an apriopriate instance of object derived from INavigationViewModel.", false)]
    [ParseChildren(true)]
    [PersistChildren(false)]
    public abstract class Navigation<T> : NavigationView<T> where T : ITreeNode<T>
    {
        public override object DataSource
        {
            get { return base.DataSource; }
            set
            {
                base.DataSource = value;
                this.Model = new NavigationViewModel(value as IEnumerable<T>, this.GetNodeStatus, this.IsNodeVisible);
            }
        }

        private class NavigationViewModel : INavigationViewModel<T>
        {
            private readonly Func<T, NavigationNodeState> getNodeStatus;
            private readonly Func<T, bool> isNodeVisible;

            public NavigationViewModel(IEnumerable<T> rootNodes, Func<T, NavigationNodeState> getNodeStatus, Func<T, bool> isNodeVisible)
            {
                this.RootNodes = rootNodes;
                this.getNodeStatus = getNodeStatus;
                this.isNodeVisible = isNodeVisible;
            }

            public IEnumerable<T> RootNodes { get; private set; }

            public NavigationNodeState GetNodeState(T node)
            {
                return this.getNodeStatus(node);
            }

            public bool IsNodeVisible(T node)
            {
                return this.isNodeVisible(node);
            }
        }

        protected override bool IsNodeVisible(T node)
        {
            return true;
        }

        protected abstract NavigationNodeState GetNodeStatus(T node);
    }
}